%macro basicData;
data bdata0;
set &bdata;
if (2011 <= year <= 2016);
if not missing(&keyvar); 

proc sort data = bdata0 nodupkey; by cik year;
proc means data = bdata0 noprint; by cik; var year;
output out = nyear n = nyear;

data bdata0; 
merge bdata0(in = in1) nyear;by cik; if in1;

data bdata0;
set bdata0;
if nyear = 6; *require all the 6 year observations are available;
run;

proc means data = bdata0 noprint; 
var &svars;
output out = bstats(drop=_TYPE_ _FREQ_) mean = &svars;
run;

proc transpose data = bstats out = bstats0; run;

data bstats0(keep = variable);
set  bstats0(keep = _NAME_ rename = (_NAME_=variable));
run;

data bstats0;
set  bstats0;
retain nn 0;
nn = nn+1;
run;

%if &PostInd = 0 %then %do;
%stat(bdata0, bstats1, &svars, n mean std min p25 p50 p75 max, bygroup=none);
%end;
%else %do;
data bdata0post;
set bdata0;
if year >= 2014;
run;
%stat(bdata0post, bstats1, &svars, n mean std min p25 p50 p75 max, bygroup=none);
%end;

proc sort data = bstats1(rename = (factor = variable)); by variable;
proc sort data = bstats0; by variable; run;

data bstats;
merge bstats0  bstats1;
by variable;

proc sort data = bstats; by nn; run;

data &bstats;
set bstats;
drop nn;
run;

proc datasets library = work nolist;
delete bdata0 bdata0post bstats0 bstats bstats1 bstats nyear; 
run;
%mend;

%macro riskratio_did33(inputdata =, baseyear =, rankvar = , riskvar = , out = , outcontinu = , outmean = , charmean =, charstd = , group_num = , rankind = 1, chars = , diff = 0, 
controls = logba profit mtob, addvar = , rankfile =);
* macro riskratio_did: calculate the mean of each group and perform diff-in-diff;

*baseyear is the event year;
*rankind = 1 if the sorting procedure is needed, = 0 if the rank is predetermined;
*rankvar is the ranking variable at the year before the event year;
*riskvar is the dependent variable, i.e., risky financial and real assets;

*output;
*outmean is the sample mean of each group;
*out is the results of Diff-in-Diff test with fixed year and firm effects, and clustered stderror;

*full sample;
data inputdata00;
set &inputdata;
if year in (2011, 2012, 2013, 2014, 2015, 2016);
if not missing (&riskvar); *and not missing(&rankvar);
run;

proc sort data = inputdata00 nodupkey; by cik year;
proc means data = inputdata00 noprint;
by cik;
var year;
output out = nyear n = nyear; run;

data inputdata00;
merge inputdata00(in = in1) nyear;by cik;
if in1;
run;

data inputdata00;
set inputdata00;
if nyear = 6; *require all the 6 year observations are available;
run;

%if %eval(&rankind) = 1 %then %do;
*full sample;
data inputdata0;
set inputdata00;
if not missing (&riskvar); * and not missing(&rankvar);
run;

*ranking sample only;
data temp_group(keep=cik year &rankvar);
set inputdata00;
if not missing (&riskvar) and not missing(&rankvar);
if year=&baseyear; *ranking year;
run;

*divide the sample into groups;
proc rank data=temp_group out=temp_group(keep=cik group) groups=&group_num;var &rankvar;ranks group;run;

data temp_group;
set temp_group;
group=group+1; 
run;

proc sort data  = inputdata0; by cik; run;
proc sort data  = temp_group; by cik; run;

*assign the ranks to the full sample;
data oilcrisis;
merge inputdata0(in = in1) temp_group;
by cik;
if in1;
if not missing(group);
run;

data oilCrisis;
set oilcrisis;
oilCrisis_year = &baseyear;

distrInd = .;
if (group = &group_num) then distrInd = 1; *group with high operating/financial liability or highly distressed;
if (group < &group_num) then distrInd = 0;
*if (1<group<&group_num) then distrInd = 0.5;
*if (group = 1) then distrInd = 0;

if missing(group) then delete;
if not missing(&riskvar);

if &baseyear = 2014 then post = (year in (2014, 2015, 2016));
if &baseyear = 2015 then post = (year in (2015, 2016));
if &baseyear = 2016 then post = (year in (2016));

logy = log(1 + &riskvar);
*logy = &riskvar;
var = log(1 + &rankvar);
*var = &rankvar;
var_post = post*var; *the interaction term of post-crisis year and the high liablity indicator;

distr_post = distrInd*post; *the interaction term of post-crisis year and the high liablity indicator;

*d2010 = (year = 2010);
*d2011 = (year = 2011);
d2012 = (year = 2012);
d2013 = (year = 2013);
d2014 = (year = 2014);
d2015 = (year = 2015);
d2016 = (year = 2016);

run;

%if (&addvar ne ) %then %do;
data oilCrisis;
set oilcrisis;
avar_post = post*&addvar;
avar_distr = distrInd*&addvar;
avar_distr_post = distr_post*&addvar;
run;
%end;

%end;
%else %if %eval(&rankind) = 2 %then %do;
*full sample;
data inputdata0;
set inputdata00;
if not missing (&riskvar) and not missing(&rankvar);
run;

*ranking sample only;
data temp_group(keep=cik year &rankvar);
set inputdata00;
if not missing (&riskvar) and not missing(&rankvar);
if year=&baseyear; *ranking year;
run;


data temp_group;
set temp_group;
group= &rankvar+1; 
if not missing(group)
drop year;
run;

proc sort data  = inputdata0; by cik; run;

*assign the ranks to the full sample;
data oilcrisis;
merge inputdata0(in = in1) temp_group(in = in2);
by cik;
if (in1 and in2);
*if not missing(group);
run;

data oilCrisis;
set oilcrisis;
oilCrisis_year = &baseyear;

distrInd = .;
if (group = &group_num) then distrInd = 1; *group with high operating/financial liability or highly distressed;
if (group < &group_num) then distrInd = 0;
*if (1<group<&group_num) then distrInd = 0.5;
*if (group = 1) then distrInd = 0;

if missing(group) then delete;
if not missing(&riskvar);

if &baseyear = 2014 then post = (year in (2014, 2015, 2016));
if &baseyear = 2015 then post = (year in (2015, 2016));
if &baseyear = 2016 then post = (year in (2016));

distr_post = distrInd*post; *the interaction term of post-crisis year and the high liablity indicator;

*d2010 = (year = 2010);
*d2011 = (year = 2011);
d2012 = (year = 2012);
d2013 = (year = 2013);
d2014 = (year = 2014);
d2015 = (year = 2015);
d2016 = (year = 2016);

run;

%if (&addvar ne ) %then %do;
data oilCrisis;
set oilcrisis;
avar_post = post*&addvar;
avar_distr_post = distr_post*&addvar;
run;
%end;

%end;
%else %if %eval(&rankind) = 0 %then %do;
data oilcrisis;
set inputdata00;
run;

data oilCrisis;
set oilcrisis;
oilCrisis_year = &baseyear;

distrInd = .;
if (group = &group_num) then distrInd = 1;
if (group < &group_num) then distrInd = 0;
*if (1<group<&group_num) then distrInd = 0.5;
*if (group = 1) then distrInd = 0;

if missing(group) then delete;
if not missing(&riskvar);

if &baseyear = 2014 then post = (year in (2014, 2015, 2016));
if &baseyear = 2015 then post = (year in (2015, 2016));
if &baseyear = 2016 then post = (year in (2016));

logy = log(1 + &riskvar);
*logy = &riskvar;
var = log(1 + &rankvar);
*var = &rankvar;
var_post = post*var; *the interaction term of post-crisis year and the high liablity indicator;	

distr_post = distrInd*post;

*d2010 = (year = 2010);
*d2011 = (year = 2011);
d2012 = (year = 2012);
d2013 = (year = 2013);
d2014 = (year = 2014);
d2015 = (year = 2015);
d2016 = (year = 2016);

run;
%end;

%if (&rankfile ne ) %then %do;
data &rankfile;
set temp_group;
run;
%end;

%if &diff = 1 %then %do;
data d00(keep = cik year distrInd &riskvar);
merge oilcrisis(keep = cik year distrInd &riskvar where = (distrInd = 0 and year = (&baseyear -1)) rename = (&riskvar = basevar))
oilcrisis(keep = cik year distrInd &riskvar where = (distrInd = 0 and year = 2016) rename = (&riskvar = basevar1));
by cik;
year = 20162013;
&riskvar = basevar1 - basevar;
run;

data d11(keep = cik year distrInd &riskvar);
merge oilcrisis(keep = cik year distrInd &riskvar where = (distrInd = 1 and year = (&baseyear -1)) rename = (&riskvar = basevar))
oilcrisis(keep = cik year distrInd &riskvar where = (distrInd = 1 and year = 2016) rename = (&riskvar = basevar1));
by cik;
year = 20162013;
&riskvar = basevar1 - basevar;
run;

data oilcrisis1; 
set oilcrisis(keep = cik year distrInd &riskvar); 
run;

proc append base = oilcrisis1 data = d00; run;
proc append base = oilcrisis1 data = d11; run;
%end;
%else  %do;
data oilcrisis1; 
set oilcrisis; 
run;
%end;

proc sort data = oilcrisis1; by distrInd year;

proc means data = oilcrisis1 noprint;
by distrInd  year;
var &riskvar;
output out = riskvarmeans(drop = _freq_ _TYPE_) mean = riskvarmean t = riskvart median = riskvarMedian n = nobs;
run;

data riskvarmeans;
set riskvarmeans; *(rename  =(_freq_ = nobs));
keep distrInd year riskvarmean riskvart nobs;
format distrInd year riskvarmean riskvart 6.3;
format nobs 2.;
run;

proc transpose data = riskvarmeans out = riskvarmeans; by distrInd; run;

data &outmean;
set riskvarmeans;
if distrInd = 1 and _Name_ = 'year' then delete;
format col1 col2 col3 col4 col5 col6 col7 6.3;
run;

%if (&chars ne ) %then %do;
proc sort data = oilcrisis; by distrInd  year;

proc means data = oilcrisis noprint;
by distrInd  year;
var &chars;
output out = charmeans(drop = _freq_ _TYPE_) mean = &chars n = nobs;
run;

data charmeans;
set charmeans;
format year 4.;
format nobs 2.;
run;

proc transpose data = charmeans(keep = distrInd year &chars nobs) out = charmeans; by distrInd; run;

data &charmean;
set charmeans;
if distrInd = 1 and _Name_ = 'year' then delete;
format col1 col2 col3 col4 col5 col6 col7 6.3;
format nobs 2.;
run;

proc means data = oilcrisis noprint;
by distrInd  year;
var &chars;
output out = charstd(drop = _freq_ _TYPE_) std = &chars n = nobs;
run;

data charstd;
set charstd;
format year 4.;
format nobs 2.;
run;

proc transpose data = charstd(keep = distrInd year &chars nobs) out = charstd; by distrInd; run;

data &charstd;
set charstd;
if distrInd = 1 and _Name_ = 'year' then delete;
format col1 col2 col3 col4 col5 col6 col7 6.3;
format nobs 2.;
run;
%end;

*obtain the results of Diff-in-Diff test with fixed year and firm effects, and clustered stderror;
*the macro FEREG_LSDV has the firm fixed effect and cluster the stderror at the firm level;

%if (&out ne ) %then %do;
data oilcrisis;
set oilcrisis;
*if year = 2014 then delete;
run;

%let controlyrs = d2012 d2013 d2015 d2016; *adding the year fixed year;

%if (&addvar ne ) %then %do;
%FEREG_LSDV(&riskvar, &rankvar var_post &addvar avar_distr avar_post avar_distr_post &controls &controlyrs, cik, year, cik, oilcrisis, didresults_&riskvar);
%end;
%else %do;
ods html close;
%FEREG(logy,distrInd post distr_post &controls &controlyrs, cik, year, cik, oilcrisis, didresults0_&riskvar);
%FEREG(logy,distrInd post distr_post &controls, cik, year, cik, oilcrisis, didresults1_&riskvar);
%FEREG_LSDV(logy, post distr_post &controls, cik, year, cik, oilcrisis, didresults2_&riskvar);
%FEREG_LSDV(logy, distr_post &controls &controlyrs, cik, year, cik, oilcrisis, didresults3_&riskvar);
ods html;
data didresults0_&riskvar;
set didresults0_&riskvar;
retain nn 1;
nn = nn + 1;
run;

data didresults2_&riskvar;
set didresults2_&riskvar;
if parameter = 'Intercept' then value = .;
if parameter = 'intercept' then value = .;
run;

data didresults3_&riskvar;
set didresults3_&riskvar;
if parameter = 'Intercept' then value = .;
if parameter = 'intercept' then value = .;
run;

proc sql;
  create table didresults_&riskvar(drop = value0)
  as select a.*, b1.*, b2.*, b3.* 
  from didresults0_&riskvar(keep = nn parameter _type value rename = (value= value0)) as a
  left JOIN
  didresults1_&riskvar(keep = parameter _type value rename = (value= value1)) as b1 
  on a.parameter =b1.parameter and a._type = b1._type
   left JOIN
  didresults2_&riskvar(keep = parameter _type value rename = (value= value2)) as b2 
  on a.parameter =b2.parameter and a._type = b2._type
    left JOIN
  didresults3_&riskvar(keep = parameter _type value rename = (value= value3)) as b3 
  on a.parameter =b3.parameter and a._type = b3._type;
quit;

/*
proc sort data = didresults0_&riskvar; by parameter _type;
proc sort data = didresults1_&riskvar; by parameter _type;
proc sort data = didresults2_&riskvar; by parameter _type;
proc sort data = didresults3_&riskvar; by parameter _type;

data didresults_&riskvar(drop = value0);
merge didresults0_&riskvar(keep = parameter _type value rename = (value= value0)) 
didresults1_&riskvar(keep = parameter _type value rename = (value= value1)) 
didresults2_&riskvar(keep = parameter _type value rename = (value= value2)) 
didresults3_&riskvar(keep = parameter _type value rename = (value= value3));
by parameter _type;
run;
*/
ods html close;
%FEREG(logy, var post var_post &controls &controlyrs, cik, year, cik, oilcrisis, didresults0_&riskvar);
%FEREG(logy, var post var_post &controls, cik, year, cik, oilcrisis, didresults1_&riskvar);
%FEREG_LSDV(logy, post var_post &controls, cik, year, cik, oilcrisis, didresults2_&riskvar);
%FEREG_LSDV(logy, var_post &controls &controlyrs, cik, year, cik, oilcrisis, didresults3_&riskvar);
ods html;
data didresults0_&riskvar;
set didresults0_&riskvar;
retain nn 1;
nn = nn + 1;
run;

data didresults2_&riskvar;
set didresults2_&riskvar;
if parameter = 'Intercept' then value = .;
if parameter = 'intercept' then value = .;
run;

data didresults3_&riskvar;
set didresults3_&riskvar;
if parameter = 'Intercept' then value = .;
if parameter = 'intercept' then value = .;
run;

proc sql;
  create table didresultsc_&riskvar(drop = value0)
  as select a.*, b1.*, b2.*, b3.* 
  from didresults0_&riskvar(keep = nn parameter _type value rename = (value= value0)) as a
  left JOIN
  didresults1_&riskvar(keep = parameter _type value rename = (value= value1)) as b1 
  on a.parameter =b1.parameter and a._type = b1._type
   left JOIN
  didresults2_&riskvar(keep = parameter _type value rename = (value= value2)) as b2 
  on a.parameter =b2.parameter and a._type = b2._type
    left JOIN
  didresults3_&riskvar(keep = parameter _type value rename = (value= value3)) as b3 
  on a.parameter =b3.parameter and a._type = b3._type;
quit;
/*
proc sort data = didresults0_&riskvar; by parameter _type;
proc sort data = didresults1_&riskvar; by parameter _type;
proc sort data = didresults2_&riskvar; by parameter _type;
proc sort data = didresults3_&riskvar; by parameter _type;

data didresultsc_&riskvar(drop = value0);
merge didresults0_&riskvar(keep = parameter _type value rename = (value= value0)) 
didresults1_&riskvar(keep = parameter _type value rename = (value= value1)) 
didresults2_&riskvar(keep = parameter _type value rename = (value= value2)) 
didresults3_&riskvar(keep = parameter _type value rename = (value= value3));
by parameter _type;
run;
*/
%end;

proc sort data  = didresults_&riskvar out = &out(drop = nn); by nn;
run;

proc sort data  = didresultsc_&riskvar out = &outcontinu(drop = nn); by nn;
run;

%end;

proc datasets library = work nolist;
delete inputdata0 inputdata00 inputdat temppp temp_group nyear didresultsc_&riskvar didresults_&riskvar 
didresults0_&riskvar didresults1_&riskvar didresults2_&riskvar didresults3_&riskvar 
d00 d11 oilcrisis charmeans riskvarme oilcrisis1; 
run;
%mend;

*compared to riskratio_did2, it adds new two lines, the difference between the high group and the low group and t-stat;
%macro riskratio_did26(inputdata =, baseyear =, rankvar = , riskvar = , out = , outcontinu = , outmean = , group_num = , rankind = 1, chars = , diff = 0, 
controls = logba profit mtob, addvar = , rankfile =);
* macro riskratio_did: calculate the mean of each group and perform diff-in-diff;

*baseyear is the event year;
*rankind = 1 if the sorting procedure is needed, = 0 if the rank is predetermined;
*rankvar is the ranking variable at the year before the event year;
*riskvar is the dependent variable, i.e., risky financial and real assets;

*output;
*outmean is the sample mean of each group;
*out is the results of Diff-in-Diff test with fixed year and firm effects, and clustered stderror;

*full sample;
data inputdata00;
set &inputdata;
*if year in (2011, 2012, 2013, 2014, 2015, 2016);
if not missing (&riskvar); *and not missing(&rankvar);
run;


%if %eval(&rankind) = 1 %then %do;
*full sample;
data inputdata0;
set inputdata00;
if not missing (&riskvar); * and not missing(&rankvar);
run;

*ranking sample only;
data temp_group(keep=cik year &rankvar);
set inputdata00;
if not missing (&riskvar) and not missing(&rankvar);
if year= %eval(&baseyear) - 1; *ranking year;
run;

*divide the sample into groups;
proc rank data=temp_group out=temp_group(keep=cik group) groups=&group_num;var &rankvar;ranks group;run;

data temp_group;
set temp_group;
group=group+1; 
run;

proc sort data  = inputdata0; by cik; run;
proc sort data  = temp_group; by cik; run;

*assign the ranks to the full sample;
data oilcrisis;
merge inputdata0(in = in1) temp_group;
by cik;
if in1;
if not missing(group);
run;

data oilCrisis;
set oilcrisis;
oilCrisis_year = &baseyear;

distrInd = .;
if (group = &group_num) then distrInd = 1; *group with high operating/financial liability or highly distressed;
if (group < &group_num) then distrInd = 0;
*if (1<group<&group_num) then distrInd = 0.5;
*if (group = 1) then distrInd = 0;

if missing(group) then delete;
if not missing(&riskvar);

if &baseyear = 2014 then post = (year in (2014, 2015, 2016));
if &baseyear = 2015 then post = (year in (2015, 2016));
if &baseyear = 2016 then post = (year in (2016));

logy = log(1 + &riskvar);
*logy = &riskvar;
var = log(1 + &rankvar);
*var = &rankvar;
var_post = post*var; *the interaction term of post-crisis year and the high liablity indicator;

distr_post = distrInd*post; *the interaction term of post-crisis year and the high liablity indicator;

*d2010 = (year = 2010);
*d2011 = (year = 2011);
d2012 = (year = 2012);
d2013 = (year = 2013);
d2014 = (year = 2014);
d2015 = (year = 2015);
d2016 = (year = 2016);

run;

%if (&addvar ne ) %then %do;
data oilCrisis;
set oilcrisis;
avar_post = post*&addvar;
avar_distr = distrInd*&addvar;
avar_distr_post = distr_post*&addvar;
run;
%end;

%end;
%else %if %eval(&rankind) = 2 %then %do;
*full sample;
data inputdata0;
set inputdata00;
if not missing (&riskvar);* and not missing(&rankvar);
run;

*ranking sample only;
data temp_group(keep=cik year &rankvar);
set inputdata00;
if not missing (&riskvar) and not missing(&rankvar);
if year=&baseyear; *ranking year;
run;


data temp_group;
set temp_group;
group= &rankvar+1; 
if not missing(group);
drop year;
run;

proc sort data  = inputdata0; by cik; run;

*assign the ranks to the full sample;
data oilcrisis;
merge inputdata0(in = in1) temp_group;
by cik;
if in1;
if not missing(group);
run;

data oilCrisis;
set oilcrisis;
oilCrisis_year = &baseyear;

distrInd = .;
if (group = &group_num) then distrInd = 1; *group with high operating/financial liability or highly distressed;
if (group < &group_num) then distrInd = 0;
*if (1<group<&group_num) then distrInd = 0.5;
*if (group = 1) then distrInd = 0;

if missing(group) then delete;
if not missing(&riskvar);

if &baseyear = 2014 then post = (year in (2014, 2015, 2016));
if &baseyear = 2015 then post = (year in (2015, 2016));
if &baseyear = 2016 then post = (year in (2016));

logy = log(1 + &riskvar);
*logy = &riskvar;		
var = log(1 + &rankvar);		
*var = &rankvar;		
var_post = post*var; *the interaction term of post-crisis year and the high liablity indicator;	

distr_post = distrInd*post; *the interaction term of post-crisis year and the high liablity indicator;

*d2010 = (year = 2010);
*d2011 = (year = 2011);
d2012 = (year = 2012);
d2013 = (year = 2013);
d2014 = (year = 2014);
d2015 = (year = 2015);
d2016 = (year = 2016);

run;

%if (&addvar ne ) %then %do;
data oilCrisis;
set oilcrisis;
avar_post = post*&addvar;
avar_distr = distrInd*&addvar;
avar_distr_post = distr_post*&addvar;
run;
%end;

%end;
%else %if %eval(&rankind) = 0 %then %do;
data oilcrisis;
set inputdata00;
run;

data oilCrisis;
set oilcrisis;
oilCrisis_year = &baseyear;

distrInd = .;
if (group = &group_num) then distrInd = 1;
if (group < &group_num) then distrInd = 0;
*if (1<group<&group_num) then distrInd = 0.5;
*if (group = 1) then distrInd = 0;

if missing(group) then delete;
if not missing(&riskvar);

if &baseyear = 2014 then post = (year in (2014, 2015, 2016));
if &baseyear = 2015 then post = (year in (2015, 2016));
if &baseyear = 2016 then post = (year in (2016));

logy = log(1 + &riskvar);	
*logy = &riskvar;		
var = log(1 + &rankvar);		
*var = &rankvar;		
var_post = post*var; *the interaction term of post-crisis year and the high liablity indicator;		
			
distr_post = distrInd*post; *the interaction term of post-crisis year and the high liablity indicator;

*d2010 = (year = 2010);
*d2011 = (year = 2011);
d2012 = (year = 2012);
d2013 = (year = 2013);
d2014 = (year = 2014);
d2015 = (year = 2015);
d2016 = (year = 2016);

run;
%end;

%if (&rankfile ne ) %then %do;
data &rankfile;
set temp_group;
run;
%end;

%if &diff = 1 %then %do;
data d00(keep = cik year distrInd &riskvar);
merge oilcrisis(keep = cik year distrInd &riskvar where = (distrInd = 0 and year = (&baseyear -1)) rename = (&riskvar = basevar))
oilcrisis(keep = cik year distrInd &riskvar where = (distrInd = 0 and year = 2016) rename = (&riskvar = basevar1));
by cik;
year = 20162013;
&riskvar = basevar1 - basevar;
run;

data d11(keep = cik year distrInd &riskvar);
merge oilcrisis(keep = cik year distrInd &riskvar where = (distrInd = 1 and year = (&baseyear -1)) rename = (&riskvar = basevar))
oilcrisis(keep = cik year distrInd &riskvar where = (distrInd = 1 and year = 2016) rename = (&riskvar = basevar1));
by cik;
year = 20162013;
&riskvar = basevar1 - basevar;
run;

data oilcrisis1; 
set oilcrisis(keep = cik year distrInd &riskvar); 
run;

proc append base = oilcrisis1 data = d00; run;
proc append base = oilcrisis1 data = d11; run;
%end;
%else  %do;
data oilcrisis1; 
set oilcrisis; 
run;
%end;

proc sort data = oilcrisis1; by distrInd year;

proc means data = oilcrisis1 noprint;
by distrInd  year;
var &riskvar;
output out = riskvarmeans(drop = _freq_ _TYPE_) mean = riskvarmean std=riskvarmeanstd t = riskvart median = riskvarMedian n = nobs;
run;

data riskvarmeans;
set riskvarmeans; *(rename  =(_freq_ = nobs));
keep distrInd year riskvarmean riskvarmeanstd riskvart nobs;
format distrInd year riskvarmean riskvarmeanstd riskvart 6.3;
format nobs 2.;
run;

data tempppp; set riskvarmeans;run;

data riskvarmeans0;
set riskvarmeans;
if distrInd=0;
run;

data riskvarmeans1;
set riskvarmeans;
if distrInd=1;
run;


data riskvarmeans0;
set riskvarmeans0;
rename riskvarmean=riskvarmean0 riskvarmeanstd=riskvarmeanstd0 riskvart=riskvart0 nobs=nobs0;
n=_n_;
run;

data riskvarmeans1;
set riskvarmeans1;
rename riskvarmean=riskvarmean1 riskvarmeanstd=riskvarmeanstd1 riskvart=riskvart1 nobs=nobs1;
n=_n_;
run;


data riskvarmeans2; 
merge riskvarmeans0 riskvarmeans1;
by n;
riskvarmean=riskvarmean1-riskvarmean0;
riskvarmeanstd=sqrt(((nobs1-1)*riskvarmeanstd1*riskvarmeanstd1+(nobs0-1)*riskvarmeanstd0*riskvarmeanstd0)/(nobs1-1+nobs0-1));
riskvart=(riskvarmean1-riskvarmean0)/(riskvarmeanstd*(sqrt(1/nobs1+1/nobs0)));
run;

proc transpose data = riskvarmeans out = riskvarmeans; by distrInd; run;

data riskvarmeans2; set riskvarmeans2;  
distrInd=2;
keep distrInd riskvarmean riskvart;
run;

proc transpose data = riskvarmeans2 out = riskvarmeans2; by distrInd; run;

data &outmean;
set riskvarmeans;
if distrInd = 1 and _Name_ = 'year' then delete;
format col1 col2 col3 col4 col5 col6 col7 6.3;
run;

data &outmean;
set &outmean riskvarmeans2;
if _name_='riskvarmeanstd' then delete;
run;



proc datasets lib=work noprint;
delete riskvarmeans0 riskvarmeans1 riskvarmeans2;
run;
quit;


%if (&chars ne ) %then %do;
proc sort data = oilcrisis; by distrInd  year;

proc means data = oilcrisis noprint;
by distrInd  year;
var &chars;
output out = charmeans mean = &chars;
run;

data riskvarmeans;
merge riskvarmeans(keep = distrInd year riskvarmean riskvart nobs) charmeans(keep = distrInd year &chars);
by distrInd year;
run;

proc transpose data = riskvarmeans out = riskvarmeans; by distrInd; run;

data &outmean;
set riskvarmeans;
if distrInd = 1 and _Name_ = 'year' then delete;
format col1 col2 col3 col4 col5 col6 col7 6.3;
run;
%end;





*obtain the results of Diff-in-Diff test with fixed year and firm effects, and clustered stderror;
*the macro FEREG_LSDV has the firm fixed effect and cluster the stderror at the firm level;

%if (&out ne ) %then %do;
data oilcrisis;
set oilcrisis;
*if year = 2014 then delete;
run;

%let controlyrs = d2012 d2013 d2015 d2016; *adding the year fixed year;

%if (&addvar ne ) %then %do;
%FEREG_LSDV(&riskvar, &rankvar var_post &addvar avar_distr avar_post avar_distr_post &controls &controlyrs, cik, year, cik, oilcrisis, didresults_&riskvar);
%end;
%else %do;

ods html close;
%FEREG(&riskvar,distrInd post distr_post &controls &controlyrs, cik, year, cik, oilcrisis, didresults0_&riskvar);
%FEREG(&riskvar,distrInd post distr_post &controls, cik, year, cik, oilcrisis, didresults1_&riskvar);
%FEREG_LSDV(&riskvar, post distr_post &controls, cik, year, cik, oilcrisis, didresults2_&riskvar);
%FEREG_LSDV(&riskvar, distr_post &controls &controlyrs, cik, year, cik, oilcrisis, didresults3_&riskvar);
ods html;

data didresults0_&riskvar;
set didresults0_&riskvar;
retain nn 1;
nn = nn + 1;
run;

data didresults2_&riskvar;
set didresults2_&riskvar;
if parameter = 'Intercept' then value = .;
if parameter = 'intercept' then value = .;
run;

data didresults3_&riskvar;
set didresults3_&riskvar;
if parameter = 'Intercept' then value = .;
if parameter = 'intercept' then value = .;
run;

proc sql;
  create table didresults_&riskvar(drop = value0)
  as select a.*, b1.*, b2.*, b3.* 
  from didresults0_&riskvar(keep = nn parameter _type value rename = (value= value0)) as a
  left JOIN
  didresults1_&riskvar(keep = parameter _type value rename = (value= value1)) as b1 
  on a.parameter =b1.parameter and a._type = b1._type
   left JOIN
  didresults2_&riskvar(keep = parameter _type value rename = (value= value2)) as b2 
  on a.parameter =b2.parameter and a._type = b2._type
    left JOIN
  didresults3_&riskvar(keep = parameter _type value rename = (value= value3)) as b3 
  on a.parameter =b3.parameter and a._type = b3._type;
quit;

/*
proc sort data = didresults0_&riskvar; by parameter _type;
proc sort data = didresults1_&riskvar; by parameter _type;
proc sort data = didresults2_&riskvar; by parameter _type;
proc sort data = didresults3_&riskvar; by parameter _type;

data didresults_&riskvar(drop = value0);
merge didresults0_&riskvar(keep = parameter _type value rename = (value= value0)) 
didresults1_&riskvar(keep = parameter _type value rename = (value= value1)) 
didresults2_&riskvar(keep = parameter _type value rename = (value= value2)) 
didresults3_&riskvar(keep = parameter _type value rename = (value= value3));
by parameter _type;
run;
*/
ods html close;
%FEREG(logy, var post var_post &controls &controlyrs, cik, year, cik, oilcrisis, didresults0_&riskvar);
%FEREG(logy, var post var_post &controls, cik, year, cik, oilcrisis, didresults1_&riskvar);
%FEREG_LSDV(logy, post var_post &controls, cik, year, cik, oilcrisis, didresults2_&riskvar);
%FEREG_LSDV(logy, var_post &controls &controlyrs, cik, year, cik, oilcrisis, didresults3_&riskvar);
ods html;
data didresults0_&riskvar;
set didresults0_&riskvar;
retain nn 1;
nn = nn + 1;
run;

data didresults2_&riskvar;
set didresults2_&riskvar;
if parameter = 'Intercept' then value = .;
if parameter = 'intercept' then value = .;
run;

data didresults3_&riskvar;
set didresults3_&riskvar;
if parameter = 'Intercept' then value = .;
if parameter = 'intercept' then value = .;
run;

proc sql;
  create table didresultsc_&riskvar(drop = value0)
  as select a.*, b1.*, b2.*, b3.* 
  from didresults0_&riskvar(keep = nn parameter _type value rename = (value= value0)) as a
  left JOIN
  didresults1_&riskvar(keep = parameter _type value rename = (value= value1)) as b1 
  on a.parameter =b1.parameter and a._type = b1._type
   left JOIN
  didresults2_&riskvar(keep = parameter _type value rename = (value= value2)) as b2 
  on a.parameter =b2.parameter and a._type = b2._type
    left JOIN
  didresults3_&riskvar(keep = parameter _type value rename = (value= value3)) as b3 
  on a.parameter =b3.parameter and a._type = b3._type;
quit;
/*
proc sort data = didresults0_&riskvar; by parameter _type;
proc sort data = didresults1_&riskvar; by parameter _type;
proc sort data = didresults2_&riskvar; by parameter _type;
proc sort data = didresults3_&riskvar; by parameter _type;

data didresultsc_&riskvar(drop = value0);
merge didresults0_&riskvar(keep = parameter _type value rename = (value= value0)) 
didresults1_&riskvar(keep = parameter _type value rename = (value= value1)) 
didresults2_&riskvar(keep = parameter _type value rename = (value= value2)) 
didresults3_&riskvar(keep = parameter _type value rename = (value= value3));
by parameter _type;
run;
*/
%end;

proc sort data  = didresults_&riskvar out = &out(drop = nn); by nn;
run;

proc sort data  = didresultsc_&riskvar out = &outcontinu(drop = nn); by nn;
run;

%end;

proc datasets library = work nolist;
delete inputdata0 inputdata00 inputdat temppp temp_group nyear didresultsc_&riskvar didresults_&riskvar 
didresults0_&riskvar didresults1_&riskvar didresults2_&riskvar didresults3_&riskvar 
d00 d11 oilcrisis charmeans riskvarme oilcrisis1; 
run;
quit;
%mend;

%macro bankruptcy(input = , keyvar = , controls =, output1 =, output2 = ,output3 = );
data tempinput;
set &input;
run;

%FEREG(log_risk_fin_assets, &keyvar &controls, cik, year, cik, tempinput, bankruptcy_regresults_log);
%FEREG(log_ttl_fin_assets, &keyvar &controls, cik, year, cik, tempinput, bankruptcy_regresults_at);
%FEREG(risk_ratio_fin, &keyvar &controls, cik, year, cik, tempinput, bankruptcy_regresults);

data bankruptcy_regresults;
set bankruptcy_regresults;
retain nn 1;
nn = nn + 1;
run;

proc sort data = bankruptcy_regresults_log; by parameter _type; 
proc sort data = bankruptcy_regresults_at; by parameter _type; 
proc sort data = bankruptcy_regresults; by parameter _type;

data bankruptcy_regresults_noBothFE;
merge bankruptcy_regresults_log(rename = (value = log_risk_fin_assets)) 
bankruptcy_regresults_at(rename = (value = log_ttl_fin_assets)) 
bankruptcy_regresults(rename = (value = risk_fin_ratio));
by  parameter _type; 
if parameter in ('d2012','d2013', 'd2014','d2015', 'd2016') then delete;
run; 

proc sort data = bankruptcy_regresults_noBothFE; by nn; run;
*data bankruptcy_regresults_noBothFE; *set bankruptcy_regresults_noBothFE; *drop nn; *run;
data &output1; set bankruptcy_regresults_noBothFE; drop nn; run;

%FEREG_LSDV(log_risk_fin_assets, &keyvar &controls, cik, year, cik, tempinput, bankruptcy_regresults_log);
%FEREG_LSDV(log_ttl_fin_assets, &keyvar &controls, cik, year, cik, tempinput, bankruptcy_regresults_at);
%FEREG_LSDV(risk_ratio_fin, &keyvar &controls, cik, year, cik, tempinput, bankruptcy_regresults);


data bankruptcy_regresults;
set bankruptcy_regresults;
retain nn 1;
nn = nn + 1;
run;

proc sort data = bankruptcy_regresults_log; by parameter _type; 
proc sort data = bankruptcy_regresults_at; by parameter _type; 
proc sort data = bankruptcy_regresults; by parameter _type;

data bankruptcy_regresults_noYrFE;
merge bankruptcy_regresults_log(rename = (value = log_risk_fin_assets)) 
bankruptcy_regresults_at(rename = (value = log_ttl_fin_assets)) 
bankruptcy_regresults(rename = (value = risk_fin_ratio));
by  parameter _type; 
if parameter in ('Intercept', 'd2012','d2013', 'd2014','d2015', 'd2016') then delete;
run; 

proc sort data = bankruptcy_regresults_noYrFE; by nn; run;
*data bankruptcy_regresults_noYrFE; *set bankruptcy_regresults_noYrFE; *drop nn; *run;
data &output2; set bankruptcy_regresults_noYrFE; drop nn; run;

%FEREG_LSDV(log_risk_fin_assets, &keyvar &controls d2015 d2016, cik, year, cik, tempinput, bankruptcy_regresults_log);
%FEREG_LSDV(log_ttl_fin_assets, &keyvar &controls d2015 d2016, cik, year, cik, tempinput, bankruptcy_regresults_at);
%FEREG_LSDV(risk_ratio_fin, &keyvar &controls d2015 d2016, cik, year, cik, tempinput, bankruptcy_regresults);


*proc print data = bankruptcy_regresults_log; *run;
*proc print data = bankruptcy_regresults; *run;

data bankruptcy_regresults;
set bankruptcy_regresults;
retain nn 1;
nn = nn + 1;
run;

proc sort data = bankruptcy_regresults_log; by parameter _type; 
proc sort data = bankruptcy_regresults_at; by parameter _type; 
proc sort data = bankruptcy_regresults; by parameter _type;

data bankruptcy_regresults_all;
merge bankruptcy_regresults_log(rename = (value = log_risk_fin_assets)) 
bankruptcy_regresults_at(rename = (value = log_ttl_fin_assets)) 
bankruptcy_regresults(rename = (value = risk_fin_ratio));
by  parameter _type; 
if parameter in ('Intercept', 'd2012','d2013', 'd2014','d2015', 'd2016') then delete;
run; 

proc sort data = bankruptcy_regresults_all; by nn; run;
*data bankruptcy_regresults_all; *set bankruptcy_regresults_all; *drop nn; *run;
data &output3; set bankruptcy_regresults_all; drop nn; run;

%mend bankruptcy;

%macro riskratio_funds(inputdata =, yy = risk_ratio_well, xx = risk_ratio_fin,  rankvar = , baseyear = , timeFE = , control = , out_subst =);
*macro riskratio_subst: perform the substitution test given different cutoffs;

%do group = 2 %to 5;
data inputdat;
set &inputdata;
run;


/**/
proc sort data = inputdat nodupkey; by cik year;
proc means data = inputdat noprint;
by cik;
var year;
output out = nyear n = nyear; run;

data inputdat;
merge inputdat(in = in1) nyear;by cik;
if in1;
run;

data inputdat;
set inputdat;
*if nyear = 6; *require all the 6 year observations are available;
run;


data temp_group(keep=cik year &rankvar);
set inputdat;
if year=&baseyear;
if not missing(&rankvar);
run;

proc rank data=temp_group out=temp_group(keep=cik rank&rankvar) groups= %eval(&group);
var &rankvar;
ranks rank&rankvar;
run;

/*
	%if &group = 2 %then %do;
	data groupss;
	set temp_group(keep = cik rank&rankvar rename = (rank&rankvar= rank&rankvar&group));
	run;
	%end;
	%else %do;
	data groupss;
	merge groupss temp_group(keep = cik rank&rankvar rename = (rank&rankvar= rank&rankvar&group));
	run;
	%end;
*/
proc sort data  = inputdat; by cik; run;
proc sort data  = temp_group; by cik; run;
data temppp;
merge inputdat(in = in1) temp_group;
by cik;
if in1;
run;


data temppp;
set temppp;
*ddtod = (missing(dtod));
*if missing(dtod) then dtod = 0;

*dnorating = missing(ratenum0);
*dbadrating = (ratenum0 > 11);

*if missing(&xx) then &xx = 0;
*missingxx = missing(&xx);

*dnorating_riskratio =dnorating*&xx;
*dbadrating_riskratio =dbadrating*&xx;

HSTDebt = (rank&rankvar = (%eval(&group)-1));
*missingRank = missing(rank&rankvar);

debtissue_HSTDebt = debtissue*HSTDebt;
debtissue1_HSTDebt = debtissue1*HSTDebt;
debtissue2_HSTDebt = debtissue2*HSTDebt;
debtissue3_HSTDebt = debtissue3*HSTDebt;
LTDebtIssue_HSTDebt = LTDebtIssue*HSTDebt;
STDebtIssue_HSTDebt = STDebtIssue*HSTDebt;
eqissue_HSTDebt = eqissue*HSTDebt;
cf_HSTDebt = cf*HSTDebt;
disinvest_HSTDebt = disinvest*HSTDebt;
capinvest_HSTDebt = capinvest*HSTDebt;
chgs_safe_fin_assets_at_HSTDebt = chgs_safe_fin_assets_at*HSTDebt;
chgs_risk_fin_assets_at_HSTDebt = chgs_risk_fin_assets_at*HSTDebt;

lchgs_safe_fin_assets_at_HSTDebt = lchgs_safe_fin_assets_at*HSTDebt;
lchgs_risk_fin_assets_at_HSTDebt = lchgs_risk_fin_assets_at*HSTDebt;

mchgs_safe_fin_assets_at_HSTDebt = log_chgs_safe_fin_assets_at*HSTDebt;
mchgs_risk_fin_assets_at_HSTDebt = log_chgs_risk_fin_assets_at*HSTDebt;

rt_risk_fin_assets_at = -(chgs_risk_fin_assets_at < 0)*chgs_risk_fin_assets_at;
rt_risk_fin_assets_at_HSTDebt = rt_risk_fin_assets_at*HSTDebt;
rt_safe_fin_assets_at = -(chgs_safe_fin_assets_at < 0)*chgs_safe_fin_assets_at;
rt_safe_fin_assets_at_HSTDebt = rt_safe_fin_assets_at*HSTDebt;

chgs_safe_fin_assets_at_HSTDebt = chgs_safe_fin_assets_at*HSTDebt;
chgs_risk_fin_assets_at_HSTDebt = chgs_risk_fin_assets_at*HSTDebt;

profit_HSTDebt = profit*HSTDebt;

post = (year in (2014, 2015, 2016));
Ddistress_riskratio_post = Ddistress_riskratio*post;
 

d2011 = (year = 2011);
d2012 = (year = 2012);
d2013 = (year = 2013);
d2014 = (year = 2014);
d2015 = (year = 2015);
d2016 = (year = 2016);
/*
Ddistress = (ratenum > 11);
if missing(ratenum) then Ddistress = 0;
Ddistress_riskratio = Ddistress*&xx;
missingRank = missing(ratenum); 


Ddistress = (mktlev0 > 0);
if missing(mktlev0) then Ddistress = 0;
Ddistress_riskratio = Ddistress*&xx;
missingRank = missing(mktlev0);

postoilcrisis = (year in (2014, 2015, 2016));
postoilcrisisxx = postoilcrisis*&xx;
*/
run;

data temppp;
set temppp;
*if year = 2014 then delete;
run;
ods html close;
%FEREG_LSDV(&yy, &xx &control &timeFE, cik, year, cik, temppp, tempresult_subst_whole);
ods html;

	%if &group = 2 %then %do;
	data result_subst_whole;
	set tempresult_subst_whole(keep = parameter value rename = (value= value&group));
	run;

	%end;
	%else %do;
	data result_subst_whole;
	merge result_subst_whole tempresult_subst_whole(keep = parameter value rename = (value= value&group));
	run;
	%end;

proc datasets library = work nolist;
delete inputdat temppp temp_group  tempresult_subst_whole; 
run;
%end;

data &out_subst; set result_subst_whole; run;

proc datasets library = work nolist;
delete inputdat temppp temp_group result_subst_whole  result_well_whole result_fin_whole tempresult_subst_whole; 
run;
%mend;

%macro riskratio_funds_HL(inputdata =, yy = risk_ratio_well, xx = risk_ratio_fin,  rankvar = , baseyear = , timeFE = , control = , out_subst =);
*macro riskratio_subst: perform the substitution test given different cutoffs;

%do group = 2 %to 5;
data inputdat;
set &inputdata;
run;


/**/
proc sort data = inputdat nodupkey; by cik year;
proc means data = inputdat noprint;
by cik;
var year;
output out = nyear n = nyear; run;

data inputdat;
merge inputdat(in = in1) nyear;by cik;
if in1;
run;

data inputdat;
set inputdat;
*if nyear = 6; *require all the 6 year observations are available;
run;


data temp_group(keep=cik year &rankvar);
set inputdat;
if year=&baseyear;
if not missing(&rankvar);
run;

proc rank data=temp_group out=temp_group(keep=cik rank&rankvar) groups= %eval(&group);
var &rankvar;
ranks rank&rankvar;
run;

/*
	%if &group = 2 %then %do;
	data groupss;
	set temp_group(keep = cik rank&rankvar rename = (rank&rankvar= rank&rankvar&group));
	run;
	%end;
	%else %do;
	data groupss;
	merge groupss temp_group(keep = cik rank&rankvar rename = (rank&rankvar= rank&rankvar&group));
	run;
	%end;
*/
proc sort data  = inputdat; by cik; run;
proc sort data  = temp_group; by cik; run;
data temppp;
merge inputdat(in = in1) temp_group;
by cik;
if in1;
run;


data temppp;
set temppp;

HSTDebt = (rank&rankvar = (%eval(&group)-1));
*missingRank = missing(rank&rankvar);
post = (year in (2014, 2015, 2016));

debtissue_post = debtissue*post;
debtissue1_post = debtissue1*post;
debtissue2_post = debtissue2*post;
debtissue3_post = debtissue3*post;
LTDebtIssue_post = LTDebtIssue*post;
STDebtIssue_post = STDebtIssue*post;

eqissue_post = eqissue*post;
cf_post = cf*post;
disinvest_post = disinvest*post;
capinvest_post = capinvest*post;

chgs_safe_fin_assets_at_post = chgs_safe_fin_assets_at*post;
chgs_risk_fin_assets_at_post = chgs_risk_fin_assets_at*post;


profit_post = profit*post;

d2011 = (year = 2011);
d2012 = (year = 2012);
d2013 = (year = 2013);
d2014 = (year = 2014);
d2015 = (year = 2015);
d2016 = (year = 2016);

run;

data temppa;
set temppp;
if HSTDebt = 0;
run;
data temppb;
set temppp;
if HSTDebt = 1;
run;


	%if &group = 2 %then %do;

	ods html close;
	%FEREG_LSDV(&yy, &xx &control &timeFE, cik, year, cik, temppp, cf_funds_risk_cond);
	%FEREG_LSDV(&yy, &xx &control &timeFE, cik, year, cik, temppa, cf_funds_risk_cond1);
	%FEREG_LSDV(&yy, &xx &control &timeFE, cik, year, cik, temppb, cf_funds_risk_cond2);
	ods html;

	data cf_funds_risk_cond;
	set cf_funds_risk_cond;
	retain nn 0;
	nn = nn + 1;
	run;

	proc sort data = cf_funds_risk_cond; by parameter _type; run;
	proc sort data = cf_funds_risk_cond1; by parameter _type; run;
	proc sort data = cf_funds_risk_cond2; by parameter _type; run;

	data cf_funds_condw;
	merge cf_funds_risk_cond(keep = nn parameter _type value rename = (value= whole))
	cf_funds_risk_cond1(keep = parameter _type value rename = (value= low&group))
	cf_funds_risk_cond2(keep = parameter _type value rename = (value= high&group));
	by  parameter _type; 
	run;

	%end;
	%else %do;

	ods html close;
	%FEREG_LSDV(&yy, &xx &control &timeFE, cik, year, cik, temppa, cf_funds_risk_cond1);
	%FEREG_LSDV(&yy, &xx &control &timeFE, cik, year, cik, temppb, cf_funds_risk_cond2);
	ods html;

	proc sort data = cf_funds_risk_cond1; by parameter _type; run;
	proc sort data = cf_funds_risk_cond2; by parameter _type; run;

	data cf_funds_condw;
	merge cf_funds_condw
	cf_funds_risk_cond1(keep = parameter _type value rename = (value= low&group))
	cf_funds_risk_cond2(keep = parameter _type value rename = (value= high&group));
	*if parameter in ('Intercept', 'd2012','d2013', 'd2014','d2015', 'd2016') then delete;
	by  parameter _type; 
	run;

	%end;

	proc datasets library = work nolist;
	delete inputdat temppp temppa temppb temp_group cf_funds_cond cf_funds_cond1 cf_funds_cond2; 
	run;
%end;

proc sort data = cf_funds_condw; by nn;
data cf_funds_condw; set cf_funds_condw; if parameter in ('Intercept', 'd2012','d2013', 'd2014','d2015', 'd2016') then delete; drop nn; run;

data &out_subst; set cf_funds_condw; run;

proc datasets library = work nolist;
delete inputdat temppp temp_group result_subst_whole  result_well_whole cf_funds_condw tempresult_subst_whole; 
run;
%mend;


